### This piece of code replicates the LCA analysis as in the paper. Notice that rerunning the LCA 
### is not required to replicate the results in the paper. In fact the folder "ChanBrexitReplication"
### already contains LCA.dta, a dataset with only the survey respondent identifiers and the class
### to which the respondent is attributed. The code here is provided for the sake of transparency.
### Notice that running this requires having created the dataset "forlatentclass.dta"  
### after independently obtaining the UKHLS data. The dataset is created and saved in the proper 
### location by the Stata do file "replication_chan_et_al_2020.do". Refer to the main script for  
### step-by-step instructions.

## you'll need the packages foreign and poLCA to run this piece of code. Install them if needed

library(foreign)

library(poLCA)

dt <- read.dta("./ChanBrexitReplication/forlatentclass.dta")

## NOTICE CHAN ET AL HAVE THE FOLLOWING
#1 Opera/operetta 
#2 Classical music performance 
#3 Rock, pop or jazz performance 
#4 Exhibition or collection of art, photography or sculpture or a
#craft exhibition (not craft market)
#5 Event which included video or electronic art 
#6 Street arts or public art display or installation (art in everyday
#surroundings, or an art work such as sculpture that is
#outdoors or in a public place)
#7 Carnival or culturally specific festival (for example, Mela,
#                                            Baisakhi, Navrati, Feis)
#8 A museum or gallery 
##"Museum or gallery" e_mla

##THESE ARE THE ITEMS INCLUDED IN LCA

#e_arts2b10 e_arts2b11 e_arts2b12 
#e_arts2a2 e_arts2a3 e_arts2a5 e_arts2a6 e_mla3


formula <- cbind(e_arts2b10 ,e_arts2b11, e_arts2b12 ,
                 e_arts2a2, e_arts2a3 ,e_arts2a5, e_arts2a6, e_mla3)~1

m1 <- poLCA(formula, data=dt,nclass=3, nrep = 3)

codings <- unique(cbind(m1$y,m1$predclass))

names(codings)[9] <- "latentclass"

dt2 <- merge(dt, codings, by=names(codings)[-9],all.x=TRUE )

tapply(dt2$e_mla3, dt2$latentclass, mean, na.rm=TRUE)


write.dta(dt2, file="../ChanBrexitReplication/LCA.dta")

poLCA.table(formula, lc=m2)

print(m2)

## turn the vars into dummies

varz <- c("e_arts2b10" ,"e_arts2b11", "e_arts2b12" ,
          "e_arts2a2", "e_arts2a3" ,"e_arts2a5", "e_arts2a6", "e_mla3")


for ( i in 1:length(varz)){

  dt2[,varz[i]] <- as.numeric(dt2[,varz[i]])  
  
}

dt2[,varz] <- dt2[,varz]-1

dt2$total <- rowSums((dt2[, c("e_arts2b10" ,"e_arts2b11", "e_arts2b12" ,
                        "e_arts2a2", "e_arts2a3" ,"e_arts2a5", 
                        "e_arts2a6", "e_mla3")]))

tapply(dt2$total, dt2$latentclass, summary)
